﻿using System;
using System.Collections.Generic;
using System.Linq;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class SearchDAO
    {
        private readonly BlueMDataContext _ctx;
        public SearchDAO()
        {
            _ctx = new BlueMDataContext();
        }

        public List<RoomType> SearchByPrice(int minPrice, int maxPrice)
        {
            try
            {
                IQueryable<RoomType> query = _ctx.RoomTypes;
                query = query.Where(p => p.RoomPrice >= minPrice && p.RoomPrice <= maxPrice);
                return query.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<RoomType> SearchByFeature(bool view, bool bath, bool balcony)
        {
            try
            {
                IQueryable<RoomType> query = _ctx.RoomTypes;
                if (view)
                    query = query.Where(p => p.View);
                if (bath)
                    query = query.Where(p => p.Bath);
                if (balcony)
                    query = query.Where(p => p.Balcony);

                return query.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<RoomType> SearchByAll(int minPrice, int maxPrice, bool view, bool bath, bool balcony)
        {
            try
            {
                IQueryable<RoomType> query = _ctx.RoomTypes;
                query = query.Where(p => p.RoomPrice >= minPrice && p.RoomPrice <= maxPrice);
                if (view)
                    query = query.Where(p => p.View);
                if (bath)
                    query = query.Where(p => p.Bath);
                if (balcony)
                    query = query.Where(p => p.Balcony);

                return query.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        //public List<int> getAvailableRoomID(int roomTypeId, DateTime inDate, DateTime outDate)
        //{
        //    try
        //    {
        //        DateTime today = DateTime.Today;

        //        var query = from p in _ctx.Rooms
        //                    where p.RoomTypeID == roomTypeId
        //                    select p.RoomID;


        //        List<int> listRoombyTypeId = query.ToList();

        //        var query2 = from p in _ctx.vSearchRooms
        //                     where p.RoomTypeID == roomTypeId
        //                           && p.CheckOutDate > today
        //                           &&
        //                           ((p.CheckOutDate > inDate && p.CheckInDate < inDate) ||
        //                            (p.CheckOutDate > outDate && p.CheckInDate < outDate))
        //                     orderby p.RoomID
        //                     select p.RoomID;

        //        List<int> listNOTAvailableRoomId = query2.ToList();

        //        foreach (int not_room in listNOTAvailableRoomId)
        //        {
        //            for (int i = 0; i < listRoombyTypeId.Count; i++)
        //            {
        //                if (not_room == listRoombyTypeId[i])
        //                {
        //                    listRoombyTypeId.RemoveAt(i);
        //                }
        //            }
        //        }
        //        return listRoombyTypeId;
        //    }
        //    catch (Exception)
        //    {
        //        return null;
        //    }
        //}

        public List<int> getUavailableRoomID(DateTime inDate, DateTime outDate)
        {
            try
            {
                var today = DateTime.Today;
                today = today.Add(new TimeSpan(12, 0, 0));
                IQueryable<vSearchRoom> query = _ctx.vSearchRooms;
                query = query.Where(p => (p.Status == 1 && inDate == today) || (p.CheckOutDate > inDate));
                query = query.OrderBy(p => p.RoomID);
                var result = query.Select(p => p.RoomID);
                return result.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<int> getAvailableRoomIdByRoomType(int roomTypeId, DateTime inDate, DateTime outDate)
        {
            try
            {
                var unav = getUavailableRoomID(inDate, outDate);
                var av = _ctx.Rooms.Where(p => p.RoomTypeID == roomTypeId).Select(p=>p.RoomID).ToList();

                foreach (var i in unav)
                {
                    av.Remove(i);
                }

                return av;
            }
            catch(Exception)
            {
                return null;
            }
        }

        public List<vRoomInfo> getAvailableRoom(int roomTypeId, DateTime inDate, DateTime outDate)
        {
            try
            {
                List<int> ListRoomID = getAvailableRoomIdByRoomType(roomTypeId, inDate, outDate);
                return ListRoomID.Select(SelectedByRoomInfoID).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        private vRoomInfo SelectedByRoomInfoID(int pRoomID)
        {
            try
            {
                IQueryable<vRoomInfo> query = _ctx.vRoomInfos;
                query = query.Where(p => p.RoomID == pRoomID);
                return query.FirstOrDefault();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
